package com.ivy.irublesson.GetConnection;

import com.ivy.irublesson.entity.TContent;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/13.
 */

public class TContentConn {
    private static String result;
    private static HttpURLConnection con;
    private static OutputStream out;

    public static String getTContentData(String teacherid , String code, String cookie)  {
        String yearId ="20170";//学期号
        String path="http://1.85.55.152/ZNPK/TeacherKBFB_rpt.aspx";//路径
        String referer="http://1.85.55.152/ZNPK/TeacherKBFB.aspx";
        String params="Sel_XNXQ="+yearId+"&Sel_JS="+teacherid+"&type=1&txt_yzm="+code;

        try{
            URL url = new URL(path);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setRequestMethod("POST");
            con.setRequestProperty("Cookie", cookie);
            con.setRequestProperty("Referer", referer);

            OutputStream out = con.getOutputStream();
            out.write(params.getBytes());
            out.flush();
            InputStream in = con.getInputStream();
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int len = 0;
            while((len=in.read(buf))!=-1){
                bout.write(buf, 0, len);
            }
            byte[] buf1 = bout.toByteArray();
            String result = new String(buf1,"GBK");

            bout.close();
            in.close();
            out.close();
            con.disconnect();
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    public static List getTContent(String teacherid , String code, String cookie) {
        String temp = getTContentData( teacherid , code, cookie);

        //String result = temp.substring(temp.indexOf("<tr ><td align=center width='5%' rowspan='2' valign=middle >"), temp.lastIndexOf("<TABLE name=theExportData WIDTH=100%"));
        List<TContent> contents = new ArrayList<>();

        Document doc= Jsoup.parse(temp);
        Elements nodes= doc.select("td[valign=top]");
        int x = 0;
        int y = 0;
        for(Element n:nodes) {
            if(y == 7) {
                //System.out.println("..........................");
                x+=1;
                y=0;
            }

            TContent content = new TContent();
            content.setInfo(n.text());
            content.setTime(x);
            content.setWeekday(y);
            //System.out.println(n.text()+"========="+x +"========="+y);
            contents.add(content);
            y += 1;
        }
        return contents;
    }

}
